package com.lanjiyin.cloud.auth.client.interceptor;

import java.io.IOException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import com.lanjiyin.cloud.auth.client.config.UserAuthConfig;
import com.lanjiyin.cloud.common.context.BaseContextHandler;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

@Component
public class OkHttpTokenInterceptor implements Interceptor {
    @Autowired
    @Lazy
    private UserAuthConfig userAuthConfig;


    @Override
    public Response intercept(Chain chain) throws IOException {
        Request newRequest = chain.request()
                    .newBuilder()
                    .header(userAuthConfig.getTokenHeader(), BaseContextHandler.getToken())
                    .build();

        Response response = chain.proceed(newRequest);
        return response;
    }
}
